<?php

class Medal_stock_in_model extends CI_Model {

    function MedalStockInModel() {
        parent::Model();
    }

    function add() {
        $return = array();
        $stock_medals = $_POST['stock-medal'];
        $stock_amounts = $_POST['stock-amount'];
        $this->db->insert('Medal_Stock_In', array(
            'startDate' => $this->db->convertDate(check_post('fromdate')),
            'endDate' => $this->db->convertDate(check_post('todate')),
            '`date`' => date("Y-m-d")
        ));
        $lastId = $this->db->insert_id();
        if ($this->db->affected_rows() > 0) {
            foreach ($stock_medals as $key => $stock_medal) {
                $this->db->insert('Medal_Stock_In_Amount', array(
                    'Medal_Stock_In_ID' => $lastId,
                    'Medal_ID' => $stock_medal,
                    'amount' => $stock_amounts[$key]
                ));
                if ($this->_isExistingMedal($stock_medal)) {
                    $this->db->query("UPDATE Medal_Stock SET amount = amount + '" . $stock_amounts[$key] . "' WHERE medal_ID='" . $stock_medal . "'  ");
                } else {
                    $this->db->insert('Medal_Stock', array(
                        'medal_ID' => $stock_medal,
                        'amount' => $stock_amounts[$key]
                    ));
                }
            }            
            $return['result'] = true;
            $return['message'] = 'ទិន្នន័យរក្សារទុកបានជោគជ័យ.';
        } else {
            $return['result'] = false;
            $return['message'] = 'មិនអាចបន្ថែមមេដាយចូលស្តុកបាន សូមព្យាយាមម្តងទៀត.';
        }
        return $return;
    }

    function getStockIn() {

        $aColumns = array('ID', 'startDate', 'endDate', 'date');
        if (isset($_GET['iSortCol_0'])) {
            for ($i = 0; $i < intval(check_get('iSortingCols')); $i++) {
                if ($_GET['bSortable_' . intval($_GET['iSortCol_' . $i])] == "true") {
                    $this->db->order_by($aColumns[intval($_GET['iSortCol_' . $i])], $_GET['sSortDir_' . $i]);
                }
            }
        } else {
            $this->db->order_by("Medal_Stock_In.ID", "ASC");
        }
        $order = $this->db->ar_orderby;
        $this->db->select('Medal_Stock_In.ID');
        $totalQuery = $this->db->get('Medal_Stock_In');

        $this->db->select('Medal_Stock_In.ID, Medal_Stock_In.startDate, Medal_Stock_In.endDate, Medal_Stock_In.date');
        $query = $this->db->get('Medal_Stock_In', check_get('iDisplayStart'), check_get('iDisplayLength'), $order);
        $medal_stocks = $query->result_array();
        $aaData = array();
        $i = check_get('iDisplayStart');
        foreach ($medal_stocks as $medal_stock) {
            $data = array(++$i, date("d/m/Y", strtotime($medal_stock['startDate'])), date("d/m/Y", strtotime($medal_stock['endDate'])), date("d/m/Y", strtotime($medal_stock['date'])), "<input type='hidden' class='id' value='{$medal_stock['ID']}' />");
            array_push($aaData, $data);
        }


        $output = array(
            "sEcho" => intval(!empty($_GET['sEcho']) ? $_GET['sEcho'] : ""),
            "iTotalRecords" => $query->num_rows(),
            "iTotalDisplayRecords" => $totalQuery->num_rows(),
            "aaData" => $aaData
        );
        return $output;
    }

    function _isExistingMedal($stockMedal) {
        $query = $this->db->get_where('Medal_Stock', array('medal_ID' => $stockMedal));
        if ($query->num_rows) {
            return true;
        } else {
            return false;
        }
    }

}

?>